$(function () {
    function bindCaptchaBtnClick() {
        $("#captcha-btn").click(function (event) {
            let $this = $(this);
            let email = $("input[name='email']").val();
             console.log("Email:", email);  // 打印邮箱值以调试
            if (!email) {
                alert("请先输入邮箱！");
                return;
            }
            // 取消按钮的点击事件
            $this.off('click');


                    // 发送ajax请求
            $.ajax('/blogauthor/captcha?email=' + email, {
                method:'GET',
                success:function (res) {
                    console.log("成功", res);
                    if (res['code'] == 200) {
                        alert("验证码发送成功！");
                    } else {
                        alert(res['msg']);
                    }
                }, fail:function (err) {
                    console.log("失败",err);

                }
            })

            // 倒计时
            let countdown = 6;
            let timer = setInterval(function () {
                if (countdown <= 0) {
                    $this.text('获取验证码');
                    // 清掉定时器
                    clearInterval(timer);
                    // 重新绑定点击事件
                    bindCaptchaBtnClick();
                } else {
                    countdown--;
                    $this.text(countdown + "s")
                }
            }, 1000);
        })
    }

    bindCaptchaBtnClick();
});
